#include "StdAfx.h"
#include "StatusMgr.h"
#include "resource.h"
#include "BBSimple.h"

#include "../_inc_util/UtilFile.h"
#include "../_inc_set/def_config.h"


CStatusMgr::CStatusMgr(void)
{
}

CStatusMgr::~CStatusMgr(void)
{
}

void CStatusMgr::Initialize()
{
	m_bOpened		= FALSE;

	m_dTZoneTb[0]	= 0;
	m_dTZoneTb[1]	= -14;
	m_dTZoneTb[2]	= -13.75;
	m_dTZoneTb[3]	= -13;
	m_dTZoneTb[4]	= -12.75;
	m_dTZoneTb[5]	= -12;
	m_dTZoneTb[6]	= -11.5;
	m_dTZoneTb[7]	= -11;
	m_dTZoneTb[8]	= -10.5;
	m_dTZoneTb[9]	= -10;
	m_dTZoneTb[10]	= -9.5;
	m_dTZoneTb[11]	= -9;
	m_dTZoneTb[12]	= -8;
	m_dTZoneTb[13]	= -7;
	m_dTZoneTb[14]	= -6.5;
	m_dTZoneTb[15]	= -6;
	m_dTZoneTb[16]	= -5.75;
	m_dTZoneTb[17]	= -5.5;
	m_dTZoneTb[18]	= -5;
	m_dTZoneTb[19]	= -4.5;
	m_dTZoneTb[20]	= -4;
	m_dTZoneTb[21]	= -3.5;
	m_dTZoneTb[22]	= -3;
	m_dTZoneTb[23]	= -2;
	m_dTZoneTb[24]	= -1;
	m_dTZoneTb[25]	= 0;
	m_dTZoneTb[26]	= 1;
	m_dTZoneTb[27]	= 2;
	m_dTZoneTb[28]	= 3;
	m_dTZoneTb[29]	= 3.5;
	m_dTZoneTb[30]	= 4;
	m_dTZoneTb[31]	= 4.5;
	m_dTZoneTb[32]	= 5;
	m_dTZoneTb[33]	= 6;
	m_dTZoneTb[34]	= 7;
	m_dTZoneTb[35]	= 8;
	m_dTZoneTb[36]	= 9;
	m_dTZoneTb[37]	= 9.5;
	m_dTZoneTb[38]	= 10;
	m_dTZoneTb[39]	= 11;
	m_dTZoneTb[40]	= 12;



	memset(&m_KConfig, 0, sizeof(KConfig));
	m_bOpenedConfig = FALSE;

	GetMyDirectory(m_tzConfigPath);
	GetMyDirectory(m_tzSelectPath);

	//change cnf file path
// 	wsprintf(m_tzViewerPath, _T("%s\\viewersetting"), theApp.GetDocPath());
// 	if (!CreateDirectory(m_tzViewerPath, NULL))
// 	{
// 		TRACE(_T("CreateDirectory- %s: GetLastError = %08X\n"), m_tzViewerPath, GetLastError());
// 	}
// 
// 	OpenKConfig();
}


void CStatusMgr::InitBlackboxSetting()
{
	// Camera
	for (int i=0; i<4; i++)
	{
		PKCamera pCamera = &m_KConfig.Camera[i];

		pCamera->Color.nBright   = 50;
		pCamera->Color.nContrast = 50;
		pCamera->Color.nBright   = 50;
		pCamera->Color.nBright   = 50;
		pCamera->Color.nBright   = 50;
	}
}

void CStatusMgr::SetConfigPath(TCHAR* tzConfigPath)
{
	CUtilFile uf;

	TCHAR tzFilePath[MAX_PATH] = {0};
	wsprintf(m_tzSelectPath, _T("%s"), tzConfigPath);
	m_bOpened = TRUE;

}

BOOL CStatusMgr::LoadBlackboxSetting()
{
	if (!m_bOpened)
		return FALSE;

	CUtilFile uf;

	TCHAR tzFilePath[MAX_PATH] = {0};

	m_bOpenedConfig = FALSE;

	wsprintf(tzFilePath, _T("%s\\config.dat"), m_tzSelectPath);
	if (!uf.Open(tzFilePath))
		return FALSE;

	if (!uf.Read(&m_KConfig, sizeof(m_KConfig)))
	{
		uf.Close();
		return FALSE;
	}

	uf.Close();
	m_bOpenedConfig = TRUE;
	return TRUE;
}

BOOL CStatusMgr::SaveBloackboxSetting()
{
	CUtilFile  uf;

	TCHAR  tzFilePath1[256] = {0};
	TCHAR  tzFilePath2[256] = {0};
	wsprintf(tzFilePath1, _T("%s\\config.dat"), m_tzConfigPath);

	if (!uf.Create(tzFilePath1))
		goto Fail;

	if (!uf.Write(&m_KConfig, sizeof(m_KConfig)))
		goto Fail;

	uf.Close();

	wsprintf(tzFilePath2, _T("%s\\config.dat"), m_tzSelectPath);

	CopyFile(tzFilePath1, tzFilePath2, FALSE);

	m_tTimezone = m_dTZoneTb[m_KConfig.System.timeinfo.nTimezone] * 3600;

	return	TRUE;

Fail:
	uf.Close();
	return	FALSE;
}

BOOL CStatusMgr::OpenKConfig()
{
	if (!LoadBlackboxSetting())
	{
		InitBlackboxSetting();	
		m_tTimezone = 0;

		return  FALSE;
	}

	m_tTimezone = m_dTZoneTb[m_KConfig.System.timeinfo.nTimezone] * 3600;

	return  TRUE;
}

void CStatusMgr::GetKConfig(PKConfig pKconfig)
{	
	memcpy (pKconfig, &m_KConfig, sizeof(m_KConfig));
}

void CStatusMgr::SetKConfig(PKConfig pKconfig)
{
	memcpy (&m_KConfig, pKconfig, sizeof(m_KConfig));

	SaveBloackboxSetting();
}

DWORD CStatusMgr::GetSpeedometer()
{
	return  m_KConfig.System.nSpeedometer;
}

void CStatusMgr::GetMyDirectory(TCHAR *tzPath)
{
	TCHAR buff[ MAX_PATH ]={0};
	GetModuleFileName(GetModuleHandle(NULL) , buff , MAX_PATH);

	CString strPath = buff ;
	int n = strPath.ReverseFind('\\');
	strPath.Delete(n , strPath.GetLength()- n);

	wsprintf(tzPath, strPath);

}

TCHAR *CStatusMgr::GetModelPath()
{
	if (!m_bOpenedConfig)
		return NULL;

	TCHAR tzModelName[32+1];
	MultiByteToWideChar(CP_ACP, 0, (CHAR *)m_KConfig.DeviceInfo.MODEL_NAME, 32+1, (LPWSTR) tzModelName, 128);
	wsprintf(m_tzModelPath, _T("%s\\model\\DEV_%s.cfg"), m_tzConfigPath, tzModelName);
	return m_tzModelPath;
}

